<template>
	<view class="upload-avatar-container">
		<!-- 头像显示区域 -->
		<view class="avatar-display" @click="chooseImage">
			<image v-if="avatarUrl" :src="avatarUrl" class="avatar-image" mode="aspectFill" />
			<view v-else class="avatar-placeholder">
				<text class="placeholder-icon">📷</text>
				<text class="placeholder-text">点击上传头像</text>
			</view>
			<!-- 上传中遮罩 -->
			<view v-if="uploading" class="uploading-mask">
				<text class="uploading-text">上传中...</text>
			</view>
		</view>
	</view>
</template>

<script setup>
	import {
		ref,
		defineEmits,
		defineProps,
		watch
	} from 'vue'
	import {
		uploadFile
	} from '@/api/upload/upload'

	// Props
	const props = defineProps({
		modelValue: {
			type: String,
			default: ''
		}
	})

	// Emits
	const emit = defineEmits(['update:modelValue', 'upload-success'])

	// 响应式数据
	const avatarUrl = ref(props.modelValue)
	const uploading = ref(false)

	// 选择图片并直接上传
	const chooseImage = () => {
		if (uploading.value) return

		uni.chooseImage({
			count: 1,
			sizeType: ['compressed'],
			sourceType: ['album', 'camera'],
			success: async (res) => {
				const tempFilePath = res.tempFilePaths[0]
				await uploadImage(tempFilePath)
			},
			fail: (err) => {
				console.error('选择图片失败:', err)
				uni.showToast({
					title: '选择图片失败',
					icon: 'none'
				})
			}
		})
	}

	// 上传图片
	const uploadImage = async (filePath) => {
		if (!filePath) return

		uploading.value = true

		try {
			const response = await uploadFile(filePath, 'minio', 'MASTER')

			if (response.code === 200 && response.url) {
				avatarUrl.value = response.url
				emit('update:modelValue', response.url)
				emit('upload-success', response.url)

				uni.showToast({
					title: '上传成功',
					icon: 'success'
				})
			} else {
				throw new Error(response.msg || '上传失败')
			}
		} catch (error) {
			console.error('上传失败:', error)
			uni.showToast({
				title: error.message || '上传失败',
				icon: 'none'
			})
		} finally {
			uploading.value = false
		}
	}

	// 监听props变化
	watch(() => props.modelValue, (newVal) => {
		avatarUrl.value = newVal
	})
</script>

<style scoped>
	.upload-avatar-container {
		display: flex;
		justify-content: center;
		align-items: center;
	}

	.avatar-display {
		position: relative;
		width: 200rpx;
		height: 200rpx;
		border-radius: 24%;
		overflow: hidden;
		border: 4rpx solid #e8e8e8;
		display: flex;
		justify-content: center;
		align-items: center;
		background: #f8f9fa;
		cursor: pointer;
		transition: all 0.3s;
	}

	.avatar-display:active {
		transform: scale(0.95);
	}

	.avatar-image {
		width: 100%;
		height: 100%;
	}

	.avatar-placeholder {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		color: #999;
	}

	.placeholder-icon {
		font-size: 48rpx;
		margin-bottom: 8rpx;
	}

	.placeholder-text {
		font-size: 24rpx;
	}

	.uploading-mask {
		position: absolute;
		top: 0;
		left: 0;
		right: 0;
		bottom: 0;
		background: rgba(0, 0, 0, 0.6);
		display: flex;
		align-items: center;
		justify-content: center;
		border-radius: 50%;
	}

	.uploading-text {
		color: white;
		font-size: 24rpx;
	}
</style>